/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
/*
 * 蛋白石组件
 */
import * as power_property from "../system/power_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
/**
 * * 召唤 渊鲸 - 维系者
 */
export function CallWhaleSupportTrigger(): server.ItemCustomComponent {
    return {
        onUse(source) {
            /**
             * * 触发自定义组件的玩家
             */
            const player = source.source;
            /**
             * * 触发自定义组件的物品
             */
            const item = source.itemStack;
            /**
             * * 玩家背包
             */
            const container = player.getComponent('inventory')?.container;
            // 判断条件是否满足
            if (!container || !player || !item) return;
            // 判断是否冷却完成
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 20)) return;
            // 检测是否在 水下
            if (!player.dimension.getBlock(player.location)?.isLiquid) {
                player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 请在水下使用, 无法在空气中召唤<§l§9 渊鲸-维系者 §r>' }]);
                return;
            };
            // 执行召唤流程
            native_tools.SummoningEntityWithData(player, container, 'entity_machine:whale_support');
        }
    }
};
/**
 * * 召唤 森蚺 - 哨兵炮
 */
export function CallPythonSentinelTrigger(): server.ItemCustomComponent {
    return {
        onUse(source) {
            /**
             * * 触发自定义组件的玩家
             */
            const player = source.source;
            /**
             * * 触发自定义组件的物品
             */
            const item = source.itemStack;
            /**
             * * 玩家背包
             */
            const container = player.getComponent('inventory')?.container;
            // 判断条件是否满足
            if (!container || !player || !item) return;
            // 判断是否冷却完成
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 20)) return;
            // 检测是否在 水下
            if (!player.dimension.getBlock(player.location)?.isLiquid) {
                player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 请在水下使用, 无法在空气中召唤<§l§9 渊鲸-维系者 §r>' }]);
                return;
            };
            // 执行召唤流程
            native_tools.SummoningEntityWithData(player, container, 'entity_machine:python_sentinel');
        }
    }
};
/**
 * * 召唤 隧龙 - 领航者
 */
export function CallTunnelDragonGuideTrigger(): server.ItemCustomComponent {
    return {
        onUse(source) {
            /**
             * * 触发自定义组件的玩家
             */
            const player = source.source;
            /**
             * * 触发自定义组件的物品
             */
            const item = source.itemStack;
            /**
             * * 玩家背包
             */
            const container = player.getComponent('inventory')?.container;
            // 判断条件是否满足
            if (!container || !player || !item) return;
            // 判断是否冷却完成
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 20)) return;
            // 检测是否在 水下
            if (!player.dimension.getBlock(player.location)?.isLiquid) {
                player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 请在水下使用, 无法在空气中召唤<§l§9 渊鲸-维系者 §r>' }]);
                return;
            };
            // 执行召唤流程
            native_tools.SummoningEntityWithData(player, container, 'entity_machine:tunnel_dragon_guide');
        }
    }
};

/**
 * * 森蚺 - 先锋炮
 */
export function CallPythonPioneerTrigger(): server.ItemCustomComponent {
    return {
        onUse(source) {
            /**
             * * 触发自定义组件的玩家
             */
            const player = source.source;
            /**
             * * 触发自定义组件的物品
             */
            const item = source.itemStack;
            /**
             * * 玩家背包
             */
            const container = player.getComponent('inventory')?.container;
            // 判断条件是否满足
            if (!container || !player || !item) return;
            // 判断是否冷却完成
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 20)) return;
            /**
             * * 实体射线选中数组
             */
            const getEntities = player.getEntitiesFromViewDirection({ maxDistance: 64 })[0];
            /**
             * * 方块射线选中
             */
            const getBlock = player.getBlockFromViewDirection({ maxDistance: 64 });
            /**
             * * 方块距离
             */
            const blockDistance = getBlock?.block ? math_tools.Vector.distance(player.location, getBlock?.block) : 48;
            /**
             * * 射击距离
             */
            const distance = getEntities?.distance ?? blockDistance;
            /**
             * * 爆炸事件
             * 
             * @param args - 附加参数
             */
            const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
                /**
                 * * 过滤器参数
                 */
                const options: server.EntityQueryOptions = {
                    excludeTypes: ["minecraft:item", "minecraft:xp_orb"],
                    excludeFamilies: ['starry'],
                    location: args.location,
                    maxDistance: 8,
                    closest: 8
                };
                /**
                 * * 获取实体列表
                 */
                const entitys = args.dimension.getEntities(options).filter(entity => entity.id !== player.id);
                // 创建 元素伤害
                entitys.forEach(
                    entity => {
                        /**
                         * * 是否暴击
                         */
                        const erupt = derived_tools.IsErupt(player);
                        /**
                         * * 击退方向
                         */
                        const direction = math_tools.Vector.difference(player.location, entity.location);
                        /**
                         * * 本体与目标的距离
                         */
                        const distance = math_tools.Vector.distance(player.location, entity.location);
                        /**
                         * * 击退系数
                         */
                        const mapping = (1 + distance / distance) * 3;
                        // 获得属性加成
                        power_property.AlterProperty(player, { raise_basic_attack: distance });
                        // 执行击退
                        entity.applyKnockback(direction.x, direction.z, mapping, direction.y * 2);
                        // 结算法术伤害
                        achieve_plan.RunePowerAttack.BriefCreate(player, entity, erupt);
                    }
                );
                // 播放 爆炸音效
                args.dimension.playSound('random.explode', args.location);
                // 创建粒子效果
                native_tools.TrySpawnParticle(player.dimension, 'constant:impact_rune_white', args.location);
            };
            // 创建 路径规划
            achieve_plan.PathExecute.Create('森蚺先锋炮-炮击轨迹', 1,
                {
                    particles: ['constant:smoke_rune_white'],
                    dimension: player.dimension,
                    on_done: powerExplode,
                    location_group: [],
                    cooldown: 1,
                    speed: 1,
                    shoot: {
                        start_place: player.getHeadLocation(),
                        toward: player.getViewDirection(),
                        max_distance: distance,
                    }
                }
            )
            // 播放 开火音效
            player.playSound('mob.irongolem.repair');
            // 更新 物品冷却
            item.getComponent('minecraft:cooldown')?.startCooldown(player);
            // 更新 物品耐久
            native_tools.SetDurability(player, item, container, player.selectedSlotIndex, 1);
        }
    }
};